<?php
  class AuthenController extends  BaseController{
		
		protected  $_className ="Users";
		
		public function login($username,$password){
			
			if(PageUtils::IsEmpty($username) || PageUtils::IsEmpty($password)){
				CustomerException::ThrowException("用户名和者密码不可为空");		
			}
			
			$users = AbstractBean::LoadBean("Users");
			$filter = new SqlFilter("userid",EQUALS,$username);
			$filter->addFilter("password",EQUALS,$password);
			if (!$users->loadFirst($filter)){
				throw new CustomerException("用户名或者密码不正确",-2);
			}
			else{
				$_SESSION["username"] =$username;
				$_SESSION["user_id"] =$users->getStringValue("id");
				$_SESSION["departmentid"] =$users->getStringValue("departmentid");
				$_SESSION["shop_id"] =$users->getStringValue("departmentid");
				$_SESSION["company_id"] =$users->getStringValue("company_id");
				$_SESSION["shop"] =$users->getStringValue("shop");
				$_SESSION["displayname"] =$users->getStringValue("diplay_name");
				$_SESSION["curt_user"] =$users;
				$_SESSION["powers"] = $users->getPowers();
			}

			 $msg = new Message();
			 $msg->username=$_SESSION["username"];
			 $msg->dept_id=$_SESSION["dept_id"];
			 $msg->company_id=$_SESSION["company_id"];
			 $msg->powers = $_SESSION["powers"];

			 return $msg;

		}

		
		public function getUsersList($filter,$state){

			return parent::getEntityList($filter,$state);
		}


		public function loginOut($username,$password){
			echo $pageNo."==============".$pageSize;
		}




		public function saveUsers($transaction=true){

			$users = AbstractBean::LoadBean(Users,1);
			$users->set("password","123456");
			$users->save();
			//throw new CustomerException("Please Login first");
		}


		public function heartBeat(){
			 $msg = new Message();
			 $msg->username=$_SESSION["username"];
			 $msg->dept_id=$_SESSION["dept_id"];
			 return $msg;
		}

		public function getWeixinSignInfo(){
			$jssdk = new JSSDK(CORPID, CORPSECRET);
			return $jssdk->getSignInfo();
		}


		public function getPowerList(){
			return $_SESSION["powers"];
		}

		public function getAvailableMenu(){
			$userPowerList = $this->getPowerList();

			$tree = array();
			$gp = AbstractBean::LoadBean("PowerGroup");
			$rows = $gp->fetchBeanList(null,null,null,true);
			for($i=0;$i<count($rows);$i++){
				$bean= $rows[$i];
				$group = array(
						"text"=>$bean->get("name"),
						"leaf"=>false,
						"expanded"=>true
				);

				$powers = $bean->getChildBeanList("Powers");

				$childList = array();
				if($powers!=null){
					for($j=0;$j<count($powers);$j++){
						$power = $powers[$j];
						if(!array_key_exists($power->get("code"), $userPowerList )){
							 continue;
						}
						$node = array(
							"text"=>$power->get("name"),
							"leaf"=>true,
							"showType"=>"panel",
							"url"=>$power->get("menu_url")
						);

						array_push($childList,$node);
					}
					$group["children"]=$childList;
					if(count($childList)>0){
						array_push($tree,$group);		
					}
				}
				
			}
			return $tree;
		}


		public function wxLogin($code,$isAuthCode=false){
			$conn = new CreateConnection();
			$token =$conn->getAccessToken(CORPID,CORPSECRET);
			$w_user = new User($token);
			$users = AbstractBean::LoadBean("Users");
			if ($isAuthCode == false){
				$userId = $w_user->getUserID($code);
				$filter = new SqlFilter("userid",EQUALS,$userId);
			}else{
				$result = $w_user->getUserID($code,true);
				if(empty($result['email'])){
					// print_r($result['userid']);
					$filter = new SqlFilter("userid",EQUALS,$result['userid']);
				}else{
					$filter = new SqlFilter("email",EQUALS,$result['email']);
				}
			}
			if (!$users->loadFirst($filter)){
				throw new CustomerException("当前用户不存在",-2);
			}
			$_SESSION["username"] =$users->get("userid");
			$_SESSION["user_id"] =$users->getStringValue("id");
			$_SESSION["departmentid"] =$users->getStringValue("departmentid");
			$_SESSION["shop_id"] =$users->getStringValue("departmentid");
			$_SESSION["shop"] =$users->getStringValue("shop");
			$_SESSION["company_id"] =$users->getStringValue("company_id");
			$_SESSION["displayname"] =$users->getStringValue("name");
			$_SESSION["curt_user"] =$users;
			$_SESSION["powers"] = $users->getPowers();

			 
		}

	}	

	

?>